package com.hnevc.foodserver.controller;


import com.hnevc.foodserver.bean.News;
import com.hnevc.foodserver.bean.PageVo;
import com.hnevc.foodserver.service.NewsService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/news")
@Api(tags = "新闻接口")
public class NewsController {
    @Autowired
    NewsService newsService;
    @PostMapping("/list")
    @ApiOperation(value = "获取所有新闻")
    public Map<String,Object> list(){
        Map<String,Object> map = new HashMap<>();
        try{
            List<News> all = newsService.list();
            map.put("success",true);
            map.put("msg","成功");
            map.put("data",all);
        } catch (Exception e) {
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }
    @PostMapping("/findone")
    @ApiOperation(value = "获取一条新闻，根据ID")
    public Map<String,Object> findone(@RequestBody News News){
        Map<String,Object> map = new HashMap<>();
        try{
            News news1 = newsService.findone(News);
            map.put("success",true);
            map.put("msg","成功");
            map.put("data",news1);
        } catch (Exception e) {
            map.put("success",false);
            map.put("msg",e.getMessage());
        }
        return map;
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除一条新闻，根据ID")
    public Map<String,Object> delete(@RequestBody News News){
        Map<String,Object> map = new HashMap<>();
        try{
            News news1 = newsService.deleteById(News);
            map.put("success",true);
            map.put("msg","成功");
            map.put("data",news1);
        } catch (Exception e) {
            map.put("success",false);
            map.put("msg",e.getMessage());
        }
        return map;
    }

    @PostMapping("/add")
    @ApiOperation(value = "发表一条新闻")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "评论" ),
            @ApiImplicitParam(name = "title", value = "标题"),
            @ApiImplicitParam(name = "content", value = "用户ID"),
            @ApiImplicitParam(name = "type", value = "内容"),
            @ApiImplicitParam(name = "date", value = "发表日期"),
    })
    public Map<String,Object> add(@RequestBody News News){
        Map<String,Object> map = new HashMap<>();
        try{
            News news1 = newsService.add(News);
            map.put("success",true);
            map.put("msg","成功");
            map.put("data",news1);
        } catch (Exception e) {
            map.put("success",false);
            map.put("msg",e.getMessage());
        }
        return map;
    }

    @PostMapping("/findbytype")
    @ApiOperation(value = "获取新闻，根据类别")
    public Map<String,Object> findByType(@RequestBody News News){
        Map<String,Object> map = new HashMap<>();
        try{
            List<News> newsList= newsService.findByType(News);
            map.put("success",true);
            map.put("msg","成功");
            map.put("data",newsList);
        } catch (Exception e) {
            map.put("success",false);
            map.put("msg",e.getMessage());
        }
        return map;
    }
    @PostMapping("/findByPage")
    @ApiOperation(value = "获取新闻,分页形式")

    public Map<String,Object> findByPage(@RequestBody PageVo pageVo){
        Map<String,Object> map = new HashMap<>();
        try{
            Sort sort = Sort.by(Sort.Direction.DESC,"id");
            PageRequest pageRequest = PageRequest.of(pageVo.getPageIndex(),pageVo.getPageSize(),sort);
            Page<News> newsList= newsService.listByPage(pageRequest);
            map.put("success",true);
            map.put("msg","成功");
            map.put("data",newsList);
        } catch (Exception e) {
            map.put("success",false);
            map.put("msg",e.getMessage());
        }
        return map;
    }
}
